package project.ia04.agents.studio.StaffAgentCommunication;

import project.ia04.utils.Data;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class ListenerStaffAgentBehaviour extends CyclicBehaviour {

	@Override
	public void action() {
		MessageTemplate template = MessageTemplate.and(
				MessageTemplate.MatchPerformative(ACLMessage.INFORM),
				MessageTemplate.MatchConversationId(Data.GETITEMS));
		ACLMessage message = this.myAgent.receive(template);
		if (message != null) {
			System.out.println("StudioAgent --> Update Main Progress Listener");
			this.myAgent.addBehaviour(new UpdateItemsBehaviour(message));
		}
		else{
			template = MessageTemplate.and(
					MessageTemplate.MatchPerformative(ACLMessage.INFORM),
					MessageTemplate.MatchConversationId(Data.NEWLVL));
			message = this.myAgent.receive(template);
			if(message != null){
				System.out.println("StudioAgent --> New Lvl");
				this.myAgent.addBehaviour(new LvlUpBehaviour(message));
			}
			else{
				template = MessageTemplate.and(
						MessageTemplate.MatchPerformative(ACLMessage.INFORM),
						MessageTemplate.MatchConversationId(Data.STAFFACTION));
				message = this.myAgent.receive(template);
				if(message != null){
					this.myAgent.addBehaviour(new SendActionMessageBehaviour(message));
				}
				else{
					template = MessageTemplate.and(
							MessageTemplate.MatchPerformative(ACLMessage.INFORM),
							MessageTemplate.MatchConversationId(Data.STAMINA));
					message = this.myAgent.receive(template);
					if(message != null){
						this.myAgent.addBehaviour(new SendStaminaBehaviour(message));
					}
					else
						block();
				}
			}
		}
	}

}
